package com.superwindcloud.markdown
import android.content.Context
import android.net.Uri
import android.os.Bundle
import android.widget.EditText
import androidx.activity.ComponentActivity
import androidx.activity.compose.rememberLauncherForActivityResult
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.activity.result.contract.ActivityResultContracts
import androidx.compose.animation.core.tween
import androidx.compose.foundation.BorderStroke
import androidx.compose.foundation.ExperimentalFoundationApi
import androidx.compose.foundation.gestures.FlingBehavior
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.pager.PageSize
import androidx.compose.material3.Button
import androidx.compose.material3.Card
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.MutableState
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.input.TextFieldValue
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.superwindcloud.markdown.ui.theme.MarkdownTheme
import  com.colintheshots.twain.MarkdownText
import com.google.accompanist.pager.ExperimentalPagerApi
import com.google.accompanist.pager.HorizontalPager
import com.google.accompanist.pager.PagerDefaults
import com.google.accompanist.pager.rememberPagerState
import com.mukesh.MarkDown
import dev.chrisbanes.snapper.ExperimentalSnapperApi
import java.io.BufferedReader
import java.io.InputStreamReader
 import com.mukesh.MarkDown
@OptIn(ExperimentalPagerApi::class, ExperimentalFoundationApi::class, ExperimentalSnapperApi::class)
@Composable
fun PagerMarkdownText(markdownList: List<String>) {
    val pagerState = rememberPagerState()

//    val customFlingBehavior = FlingBehavior { scrollScope, velocity ->
//        val adjustedVelocity = velocity * 2.5f
//        scrollScope.scrollBy(adjustedVelocity)
//        scrollScope.scrollBy(adjustedVelocity, tween(durationMillis = 100))
//        abs(adjustedVelocity) > 0f
//    }

    HorizontalPager(
        count = markdownList.size,
        state = pagerState,
        modifier = Modifier.fillMaxSize(),
        flingBehavior = PagerDefaults.flingBehavior(
            state = pagerState,
            snapAnimationSpec = tween(durationMillis = 200), // 调整动画速度
        )
    ) { page ->
        MarkdownText(
            markdown = markdownList[page],
            modifier = Modifier.padding(8.dp)
        )
    }
}

class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {

            val exampleText = stringResource(R.string.example_markdown_text)
            val textFieldValue  : MutableState<TextFieldValue> = rememberSaveable(stateSaver = TextFieldValue.Saver) {
                mutableStateOf(TextFieldValue(exampleText))
            }
            val view = remember { mutableStateOf<EditText?>(null) }
            val filePickerLauncher = rememberLauncherForActivityResult(
                contract = ActivityResultContracts.GetContent(),
                onResult = { uri: Uri? ->
                    uri?.let {
                        // 处理选择的文件
                        val fileContent = readFileContent(uri)
                        textFieldValue.value = textFieldValue.value.copy(text = fileContent)
                    }
                }
            )
            MarkdownTheme {
                Column(modifier = Modifier.padding(16.dp)) {
                    Button(
                        onClick = {
                            // 启动文件选择器
                            filePickerLauncher.launch("*/*")
                        },
                        modifier = Modifier
                            .fillMaxWidth()
                            .padding(bottom = 16.dp)
                    ) {
                        Text("Open File Explorer")
                    }

                    MarkDown(
                        text = textFieldValue.value.text,
                        modifier = Modifier.fillMaxSize()
                    )
//                    MarkdownText(
//                        markdown = textFieldValue.value.text,
//                        modifier = Modifier
//                            .fillMaxWidth()
//                            .padding(16.dp)
//                            .weight(1f)
//                    )
                }
            }
        }
    }
}


fun Context.readFileContent(uri: Uri): String {
    return contentResolver.openInputStream(uri)?.use { inputStream ->
        val reader = BufferedReader(InputStreamReader(inputStream))
        val stringBuilder = StringBuilder()
        var line: String?
        while (reader.readLine().also { line = it } != null) {
            stringBuilder.append(line).append("\n")
        }
        stringBuilder.toString()
    } ?: ""
}

